<?php


namespace app\model;

use think\Model;

class AdminRole extends Model
{
    public $id;
    public $roleId;
    public $roleName;
    public $disableState;

    private $data;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->data = array();
    }

    public function __destruct()
    {
        parent::__destruct();
        unset(
            $this->data
        );
    }

    public function read()
    {
        return $this->where('delete_state', 0)
            ->field('id, role_name')
            ->select()->toArray();
    }

    public function queryRole()
    {
        return $this->where('delete_state', 0)
            ->field('id, role_name, disable_state')
            ->select()->toArray();
    }

    public function queryUsersRole()
    {
        $roleList = explode('|', $this->roleId);
        foreach ($roleList as $value)
        {
            $this->data[] = $this->where('delete_state', 0)
                ->where('id', $value)
                ->field('role_name')
                ->find()->toArray();
        }
        return $this->data;
    }

    public function deleteAdminRole()
    {
        $this->data['delete_state'] = 1;
        $this->data['delete_time'] = time();
        return $this->where('id', $this->id)
            ->save($this->data);
    }

    public function updateDisableState()
    {
        $this->data['disable_state'] = $this->disableState;
        $this->data['disable_time'] = time();
        return $this->where('id', $this->id)
            ->save($this->data);
    }

    public function addAdminRole()
    {
        $this->data['role_name'] = $this->roleName;
        $this->data['create_time'] = time();
        $this->data['update_time'] = time();
        return $this->insertGetId($this->data);
    }

    public function editAdminRole()
    {
        $this->data['role_name'] = $this->roleName;
        $this->data['update_time'] = time();
        return $this->where('id', $this->id)
            ->save($this->data);
    }

    public function queryData()
    {
        return $this->where('id', $this->id)
            ->find()->toArray();
    }
}